using System;
using System.Collections.Generic;
using UnityEngine;

public class EE_Player : MonoBehaviour
{
    private static EE_Player instance = null; public static EE_Player Instance {get{return instance;}}
    private long playerId;

    private void Awake()
    {
        instance = this;
    }

    private void Start()
    {
        SyncData();
    }

    private void SyncData()
    {
        int goldSum = 0;
        int goldCapacity = 0;
        int elixerSum = 0;
        int elixerCapacity = 0;
        // todo: 根据 playerId 查询数据库 已有建筑列表
        List<EE_Data.Building> buildings = new List<EE_Data.Building>();
        EE_Data.Building townHall = new EE_Data.Building();
        townHall.tableId = 1;
        townHall.id = EE_Data.BuildingID.townhall;
        townHall.level = 1;
        townHall.x = 30;
        townHall.y = 30;
        townHall.rows = 3;
        townHall.cols = 3;
        townHall.storage = 1000;
        townHall.capacity = 2000;
        townHall.boost = DateTime.Now;
        buildings.Add(townHall);
        EE_Data.Building goldmine1 = new EE_Data.Building();
        goldmine1.tableId = 2;
        goldmine1.id = EE_Data.BuildingID.goldmine;
        goldmine1.level = 1;
        goldmine1.x = 27;
        goldmine1.y = 27;
        goldmine1.rows = 3;
        goldmine1.cols = 3;
        goldmine1.storage = 100;
        goldmine1.capacity = 500;
        goldmine1.boost = DateTime.Now;
        buildings.Add(goldmine1);
        // 将数据库已有的建筑 加载显示到网格指定位置
        if (buildings != null)
        {
            foreach (EE_Data.Building building in buildings)
            {
                EE_Building_Controller prefab = EE_UI_Main.Instance.GetBuildingPrefab(building.id);
                if (prefab)
                {
                    EE_Building_Controller b = Instantiate(prefab, Vector3.zero, Quaternion.identity);
                    b.id = building.id;
                    b.IsConfirmed = true;
                    b.Rows = building.rows;
                    b.Cols = building.cols;
                    b.data = building;
                    b.PlaceOnGrid(building.x, building.y);
                    // 添加到已有建筑列表
                    EE_UI_Main.Instance.grid.buildings.Add(b);
                    // 设置建筑升级进度条
                    b.buildBar = Instantiate(EE_UI_Main.Instance.buildBar, EE_UI_Main.Instance.buttonsParent);
                    b.buildBar.building = b;
                    b.buildBar.gameObject.SetActive(false);
                    // 针对不同的建筑做 库存、容量、按钮 的设置计算
                    switch (building.id)
                    {
                        case EE_Data.BuildingID.goldmine:
                            // 金矿添加收集按钮
                            if (b.collectButton == null)
                            {
                                b.collectButton = Instantiate(
                                    EE_UI_Main.Instance.buttonCollectGold,
                                    EE_UI_Main.Instance.buttonsParent);
                                b.collectButton.button.onClick.AddListener(b.Collect);
                            }
                            break;
                        case EE_Data.BuildingID.goldstorage:
                            // 金币库存和容量
                            goldSum += building.storage;
                            goldCapacity += building.capacity;
                            break;
                        case EE_Data.BuildingID.elixerstorage:
                            // 圣水库存和容量
                            elixerSum += building.storage;
                            elixerCapacity += building.capacity;
                            break;
                    }
                    // 调整UI_Button的显示和隐藏
                    b.AdjustUI();
                }
            }
        }

        EE_UI_Main.Instance.goldText.text = 1000.ToString();//goldSum + "/" + goldCapacity;
        EE_UI_Main.Instance.elixerText.text = 1000.ToString();//elixerSum + "/" + elixerCapacity);
    }
}